package cn.smile.service.common;

import java.util.List;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaBuilder.In;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;

import org.springframework.stereotype.Service;

import cn.smile.bean.PageBean;
import cn.smile.dao.common.ReportGoodsDao;
import cn.smile.entity.common.Goods;
import cn.smile.entity.common.ReportGoods;
import cn.smile.entity.common.Student;

/**
 * 举报物品管理service
 */
@SuppressWarnings("SpellCheckingInspection")
@Service
public class ReportGoodsService {

    @Resource
    private ReportGoodsDao reportGoodsDao;

    /**
     * 物品添加/编辑，当id不为空时，则编辑
     */
    public ReportGoods save(ReportGoods reportGoods) {
        return reportGoodsDao.save(reportGoods);
    }


    /**
     * 物品举报信息删除
     */
    public void delete(Long id) {
        reportGoodsDao.deleteById(id);
    }


    /**
     * 根据学生查找物品
     */
    public List<ReportGoods> findByStudent(Student student) {
        return reportGoodsDao.findByStudent(student);
    }

    /**
     * 根据学生id和物品id查询
     */
    public ReportGoods find(Long goodsId, Long studentId) {
        return reportGoodsDao.find(goodsId, studentId);
    }

    /**
     * 根据id查询
     */
    public ReportGoods find(Long id) {
        return reportGoodsDao.find(id);
    }

    /**
     * 物品举报信息搜索
     */
    public PageBean<ReportGoods> findlist(PageBean<ReportGoods> pageBean, ReportGoods reportGoods, List<Goods> goodsList) {

        Specification<ReportGoods> specification = new Specification<ReportGoods>() {
            /**
             *
             */
            private static final long serialVersionUID = 1L;

            @SuppressWarnings("DuplicatedCode")
            @Override
            public Predicate toPredicate(Root<ReportGoods> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Predicate predicate = criteriaBuilder.like(root.get("content"), "%" + (reportGoods.getContent() == null ? "" : reportGoods.getContent()) + "%");
                if (reportGoods.getStudent() != null && reportGoods.getStudent().getId() != null) {
                    Predicate eqal1 = criteriaBuilder.equal(root.get("student"), reportGoods.getStudent().getId());
                    predicate = criteriaBuilder.and(predicate, eqal1);
                }
                if (goodsList != null && !goodsList.isEmpty()) {
                    In<Object> in = criteriaBuilder.in(root.get("goods"));
                    in.value(goodsList);
                    predicate = criteriaBuilder.and(predicate, in);
                }
                return predicate;
            }
        };
        Sort sort = Sort.by(Direction.DESC, "createTime");
        PageRequest pageable = PageRequest.of(pageBean.getCurrentPage() - 1, pageBean.getPageSize(), sort);
        Page<ReportGoods> findAll = reportGoodsDao.findAll(specification, pageable);
        pageBean.setContent(findAll.getContent());
        pageBean.setTotal(findAll.getTotalElements());
        pageBean.setTotalPage(findAll.getTotalPages());
        return pageBean;
    }
}
